﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using PipeAuto.DAL;
using PipeAuto.DAL.PipeAutoDataSetTableAdapters;

namespace PipeAuto.WebService
{
    /// <summary>
    /// Summary description for StockServices
    /// </summary>
    [WebService(Namespace = "http://PipeAuto.org/",Description="库存管理WebServices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class StockServices : System.Web.Services.WebService
    {

        [WebMethod(Description="测试服务")]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(Description="测试数据库")]
        public DateTime HelloDataBase()
        {
            try
            {
                DataBaseDateTableAdapter myAdapter = new DataBaseDateTableAdapter();
                return myAdapter.GetData()[0].DBDate;
            }
            catch (System.Exception ex)
            {
                return DateTime.MinValue;
            }
        }

        [WebMethod(Description="入库服务")]
        public int StockIn(string category, string trademark, string partialNo, string color, string yymm, string dd, string pipelineCode, string serialNo,string barCode)
        {

            //parameter invalidate
            if (barCode!=category+trademark+partialNo+yymm+dd+pipelineCode+serialNo+color)
            {
                return -3;
            }
            
            try
            {
                StockTableAdapter myAdapter = new StockTableAdapter();


                
                //check barcode's validation
                var myTable = myAdapter.GetDataByCombo(category, trademark, partialNo, color);

                //no such combination
                if (myTable.Count==0)
                {
                    return 1;
                }

                //disabled
                if (myTable[0].State!=1)
                {
                    return 2;
                }

                //check for redundant barcode
                StockInsTableAdapter myStockAdapter = new StockInsTableAdapter();
                if (myStockAdapter.ScalarByBarcode(barCode)>0)
                {
                    return 11;
                }

                //stock in
                myAdapter.SingleStockIn(barCode, category, trademark, partialNo, serialNo, pipelineCode, yymm, dd, color);

                //increse stock
                //myAdapter.SingleStockIn(barCode, myTable[0].ID);

            }
            catch (System.Data.SqlClient.SqlException)
            {
                //db error
                return -2;
            }
            catch (System.Exception ex)
            {
                return -1;
            }
            return 0;
        }
    }
}
